﻿using System;
using System.Data;
using System.Collections;
using System.Linq;
using BAL;

namespace App_Code
{
    class ComItem
    {
        public string Idpr;
        public int Count; 
        public ComItem(string idpr, int count)
        {
            Idpr = idpr;
            Count = count;
        }
    }
   
    public class Cart
    {

       
        private ArrayList listPro = new ArrayList(); 
        private double TM; 
        public void AddPro(string idpr, int count)
        {
            ComItem itemPro = null;
            var check = false;
            for (int i = 0; i < listPro.Count; i++)
            {
                itemPro = (ComItem)listPro[i];
                if (itemPro.Idpr.Contains(idpr))
                {
                    check = true;
                    break;
                }
            }
            if (check)
            {
                itemPro.Count += count;
            }
            else
            {
                itemPro = new ComItem(idpr, count);
                listPro.Add(itemPro);
            }
        }
        public string GetImagePro(string idpr)
        {
            var db = new dulieuDataContext();
            return (from o in db.Products where o.ProductID == Convert.ToDecimal(idpr) select o.Image).SingleOrDefault();
        }
        public int? GetIdNhomSp(string idpr)
        {
            var db = new dulieuDataContext();
            return (from o in db.Products where o.ProductID == Convert.ToDecimal(idpr) select o.Cate_ID).SingleOrDefault();
        }
        public int GetTotalPro()
        {
            return listPro.Count;
        }
        public double GetPrice(string idpr)
        {
            var db = new dulieuDataContext();
            return Convert.ToDouble((from o in db.Products where o.ProductID == Convert.ToDecimal(idpr) select o.PriceOnline).SingleOrDefault());
        }
        public string GetNamePro(string idpr)
        {
            var db = new dulieuDataContext();
            return (from o in db.Products where o.ProductID == Convert.ToDecimal(idpr) select o.Name).SingleOrDefault();
        }
        public string GetIdpr(int index)
        {
            string id = "";
            try
            {
                var item = (ComItem)listPro[index];
                id = item.Idpr;
            }
            catch { }
            return id;
        }
        public int GetQuantity(int index)
        {
            var id = 0;
            try
            {
                var item = (ComItem)listPro[index];
                id = item.Count;
            }
            catch { }
            return id;
        }
        public double GetTotalPrice()
        {
            TM = 0;
            ComItem citem;
            try
            {
                for (int i = 0; i < listPro.Count; i++)
                {
                    citem = (ComItem)listPro[i];
                    TM += citem.Count * GetPrice(Convert.ToString(citem.Idpr));
                }
            }
            catch { }
            return TM;
        }
        public void ClearAll()
        {
            listPro.Clear();
        }
        public void UpdatePro(string idpr, int total)
        {
            ComItem itemPro;
            for (var i = 0; i < listPro.Count; i++)
            {
                itemPro = (ComItem)listPro[i];
                if (itemPro.Idpr.Contains(idpr))
                {
                    itemPro.Count = total;
                    break;
                }

            }
        }
        public void RemoveItem(string idpr)
        {
            ComItem itemPro;
            for (int i = 0; i < listPro.Count; i++)
            {
                itemPro = (ComItem)listPro[i];

                if (itemPro.Idpr.Contains(idpr))
                {
                    listPro.RemoveAt(i);
                }
            }
        }
        public bool Exp(int thang)
        {
            return DateTime.Now.Month != thang;
        }

        public DataTable ToDataTable()
        {
            var dt = new DataTable();
            dt.Columns.Add("Idpr", System.Type.GetType("System.String"));
            dt.Columns.Add("Image", System.Type.GetType("System.String"));
            dt.Columns.Add("Name", System.Type.GetType("System.String"));
            dt.Columns.Add("Total", System.Type.GetType("System.String"));
            dt.Columns.Add("Price", System.Type.GetType("System.String"));
            dt.Columns.Add("Summoney", System.Type.GetType("System.String"));

            ComItem itemPro;
            DataRow RowItempro;

            for (int i = 0; i < listPro.Count; i++)
            {
                itemPro = (ComItem)listPro[i];
                RowItempro = dt.NewRow();
                RowItempro["Idpr"] = itemPro.Idpr;
                RowItempro["Image"] = GetImagePro(itemPro.Idpr.ToString());
                RowItempro["Name"] = GetNamePro(itemPro.Idpr.ToString());
                RowItempro["Total"] = itemPro.Count;
                RowItempro["Price"] = GetPrice(itemPro.Idpr.ToString());
                RowItempro["Summoney"] = itemPro.Count * GetPrice(itemPro.Idpr.ToString());
                dt.Rows.Add(RowItempro);
            }
            return dt;

        }

    }
}

